import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * @(#) File.java
 */

public class File2
{
	public String Path;
	
	public Project ReadIn;
	
	public void FindFile( )
	{
		try{
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream(this.Path);
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
              // Print the content on the console
              System.out.println (strLine);
              ParseFile(strLine);
            }
            //Close the input streams
            fstream.close();
            }catch (Exception e){//Catch exception if any
              System.out.println("Error: " + e.getMessage());
              readFilePath();
            }
	}
	public void readFilePath() {
		// TODO Auto-generated method stub
		InputStreamReader in = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(in);
		String filepath = "";
		System.out.println("Please enter file path:");
		
		try {
	         filepath = br.readLine();
	      } catch (IOException ioe) {
	         System.out.println("IO error trying to read file path!");
	      }

	    System.out.println("Thanks for the path - " + filepath);
	    
	     if (filepath != null){
	    	 Path = filepath;
	    	 FindFile();
	     }
	}
	
	public void ParseFile(String fileRow)
	{
		/**
		 * Example input file content:
			start,0,task2,task1
			task2,3,task3
			task1,4,task3,task4
			task3,2,finalTask
			task4,1,finalTask
			finalTask,3
		*/
		String[] values2 = fileRow.split(",");
		Task t = new Task();
		t.Add(values2[0], Integer.valueOf(values2[1]));
		if(values2.length > 2){
			for(int i = 2; i < values2.length; i++){ 
				for(int a = 0; a < ReadIn.tasks.size(); a++){
					if(ReadIn.tasks.get(a).TaskName.equals(values2[i])){
						PrecedenceRelations p = new PrecedenceRelations();
						p.AddRelation(values2[0]);
						ReadIn.tasks.get(a).Create.add(p);
					}
				}
			}
		}
		ReadIn.tasks.add(t);
		
	}
	public void readFilePath(String path2) {
		if (path2 != null){
	    	 Path = path2;
	    	 FindFile();
	     }
	}
}
